﻿Imports Heren.Common.Forms.Editor
Imports Heren.Common.Forms
Imports Heren.Common.ScriptEngine.Script
Imports Microsoft.VisualBasic
Imports System.Windows.Forms
Imports Heren.Common.Forms.Runtime
Imports System.Text
Imports Heren.Common.Libraries
Public Class 跌倒_坠床事件统计分析 : Inherits AbstractRuntimeHandler

    ''****************************************************************
    '' 这是护理电子病历表单后台运行支持脚本
    '' 注意：这是Visual Basic脚本，您可以不区分大小写 
    '' 请不要修改系统提供的方法名，以及方法名前面的Overrides关键字
    '' Script Language : Visual Basic
    '' Author : YangMingkun, Date : 2012-3-18
    '' Copyright (C) Heren Health Services (SUPCON) Co.,Ltd.
    ''****************************************************************


    '表单脚本初始化
    Public Overrides Sub Initialize()
        'Me.ShowMessage("表单已完成加载!")
        '在此函数中来查询和设置表单中的相关数据
        '可以调用GetSystemContext函数获取系统中已有数据，也可以从数据库中读取其他任意数据
        Dim parent As Control = Me.FindControl("RootControl1")
        If (parent IsNot Nothing) Then
            parent.Dock = DockStyle.Fill
        End If
    End Sub

    Dim dtDepts As DataTable = Nothing

    '获取关键数据列表
    Public Overrides Function GetKeyDataList() As List(Of KeyData)
        Dim keyDataList As New List(Of KeyData)
        Return keyDataList
    End Function


    '更新表单中指定的数据
    Public Overrides Function UpdateFormData(ByVal param As String, ByVal data As Object) As Boolean

        Return True
    End Function


    '获取表单中指定的数据
    Public Overrides Function GetFormData(ByVal param As Object) As Object
        If param = "表单摘要" Then
            Return Me.GetFormSummary()
        End If
        Return Nothing
    End Function


    '获取表单摘要数据
    Public Function GetFormSummary() As String
        Dim control As Control = Me.FindControl("XTextBox1")
        If control IsNot Nothing Then
            Return control.Text
        End If
        Return String.Empty
    End Function

    '病区选择时刷新列表和统计信息
    Private Sub Refresh()
        SetCursor("XGroupBox1", True)
        '    showmessage(GetSearchCondition())
        '1.根据病区读取病人数据
        Dim dtPatientInfos As DataTable = Nothing
        dtPatientInfos = Me.GetPatientInfos()
        If dtPatientInfos Is Nothing Then
            SetCursor("XGroupBox1", False)
            Return
        End If
        '     Dim dtPatientInfosFuul As DataTable = FillPatientInfos(dtPatientInfos)
        '   '2.刷新病人列表
        Me.RefreshDataGrid(dtPatientInfos)
        '3.显示需要显示的科室
        Me.ShowSelectWard()
        SetCursor("XGroupBox1", False)
    End Sub

    Private Function ShowSelectWard()
        Dim XDataGrid1 As XDataGrid = Me.FindControl("XDataGrid1")
        Dim dtCurrentWardInfos As DataTable = Nothing
        Dim XTextBox1 As XTextBox = Me.FindControl("XTextBox1")
        If XTextBox1 IsNot Nothing Then
            dtCurrentWardInfos = XTextBox1.Tag
        End If
        If dtCurrentWardInfos IsNot Nothing AndAlso dtCurrentWardInfos.Rows.Count > 0 Then
            For iGridRowIndex As Integer = 0 To XDataGrid1.rows.count - 1
                Dim szDeptCode As String = Me.GetCellValue(XDataGrid1, iGridRowIndex, 7).Trim
                Dim bShow As Boolean = False
                For Each dr As DataRow In dtCurrentWardInfos.Rows
                    If GetFieldValue(dr, "dept_code").trim = szDeptCode.Trim Then
                        bShow = True
                    End If
                Next
                If Not bShow Then

                    If XDataGrid1.rows.count > 0 And szDeptCode <> "" Then
                        '    				showmessage(iGridRowIndex.tostring & "去除" & szDeptCode)
                        XDataGrid1.Rows.RemoveAt(iGridRowIndex)
                        iGridRowIndex -= 1
                    End If
                End If
            Next iGridRowIndex
        End If
    End Function

    Private Function RefreshDataGrid(ByVal dtPatientInfos As DataTable)
        Dim XDataGrid1 As XDataGrid = Me.FindControl("XDataGrid1")
        XDataGrid1.rows.Clear()
        Dim szWardCode As String = GetFieldValue(dtPatientInfos, 0, "WARD_CODE")
        Dim szWardName As String = GetFieldValue(dtPatientInfos, 0, "WARD_NAME")
        Dim rowIndex As Integer = XDataGrid1.Rows.Add()
        Dim disChargeNum As Integer = 0
        Dim numHightScore As Integer = 0
        Dim numFallReport As Integer = 0
        Dim numFallClass3 As Integer = 0
        For index As Integer = 0 To dtPatientInfos.Rows.Count - 1
            '		showmessage(GetFieldValue(dtPatientInfos,index,"WARD_NAME"))
            If GetFieldValue(dtPatientInfos, index, "WARD_CODE") = szWardCode Then
                disChargeNum += 1
                '			showmessage(GetFieldValue(dtPatientInfos,0,"FallRiskScore"))
                If Not String.IsNullOrEmpty(GetFieldValue(dtPatientInfos, 0, "FallRiskScore")) _
                AndAlso Double.Parse(GetFieldValue(dtPatientInfos, 0, "FallRiskScore")) >= 4 Then
                    numHightScore += 1
                End If

                If Not String.IsNullOrEmpty(GetFieldValue(dtPatientInfos, 0, "FallReport")) Then
                    numFallReport += 1
                End If

                If Not String.IsNullOrEmpty(GetFieldValue(dtPatientInfos, 0, "FallClass")) _
                AndAlso Double.Parse(GetFieldValue(dtPatientInfos, 0, "FallClass")) = 3 Then
                    numFallClass3 += 1
                End If
                '			showmessage(disChargeNum)
            Else
                '			showmessage("")
                Me.SetCellValue(XDataGrid1, rowIndex, 0, szWardName)
                Me.SetCellValue(XDataGrid1, rowIndex, 1, disChargeNum.ToString)
                Me.SetCellValue(XDataGrid1, rowIndex, 2, numHightScore.ToString)
                Me.SetCellValue(XDataGrid1, rowIndex, 3, numFallReport.ToString)
                Me.SetCellValue(XDataGrid1, rowIndex, 4, (Math.Round(numHightScore / disChargeNum, 2) * 100).ToString & "%")
                Me.SetCellValue(XDataGrid1, rowIndex, 5, numFallClass3.ToString)
                If numFallReport <> 0 Then
                    Me.SetCellValue(XDataGrid1, rowIndex, 6, (Math.Round(numFallClass3 / numFallReport, 2) * 100).ToString & "%")
                Else
                    Me.SetCellValue(XDataGrid1, rowIndex, 6, "0%")
                End If
                Me.SetCellValue(XDataGrid1, rowIndex, 7, GetFieldValue(dtPatientInfos, index - 1, "WARD_CODE"))
                rowIndex = XDataGrid1.Rows.Add()
                szWardCode = GetFieldValue(dtPatientInfos, index, "WARD_CODE")
                szWardName = GetFieldValue(dtPatientInfos, index, "WARD_NAME")
                disChargeNum = 0
                numHightScore = 0
                numFallReport = 0
                numFallClass3 = 0
                index -= 1
            End If

            '		showmessage(GetFieldValue(dtPatientInfos.Tables(0),index1,"data_value"))
            '		Dim rowIndex As Integer = dataGrid.Rows.Add()
            '		Me.SetCellValue(dataGrid,rowIndex,2,dtPatientInfos)
        Next index
        Me.SetCellValue(XDataGrid1, rowIndex, 0, szWardName)
        Me.SetCellValue(XDataGrid1, rowIndex, 1, disChargeNum.ToString)
        Me.SetCellValue(XDataGrid1, rowIndex, 2, numHightScore.ToString)
        Me.SetCellValue(XDataGrid1, rowIndex, 3, numFallReport.ToString)
        Me.SetCellValue(XDataGrid1, rowIndex, 4, (Math.Round(numHightScore / disChargeNum, 2) * 100).ToString & "%")
        Me.SetCellValue(XDataGrid1, rowIndex, 5, numFallClass3.ToString)
        If numFallReport <> 0 Then
            Me.SetCellValue(XDataGrid1, rowIndex, 6, (Math.Round(numFallClass3 / numFallReport, 2) * 100).ToString & "%")
        Else
            Me.SetCellValue(XDataGrid1, rowIndex, 6, "0%")
        End If
        Me.SetCellValue(XDataGrid1, rowIndex, 7, GetFieldValue(dtPatientInfos, dtPatientInfos.Rows.Count - 1, "WARD_CODE"))
    End Function

    Private Function FillPatientInfos(ByVal dtPatientInfos As DataTable) As DataTable
        If dtPatientInfos Is Nothing Then
            Return Nothing
        End If
        Dim strSql As New StringBuilder
        strSql.Append(" select n.patient_id, n.visit_id, n.data_name, n.data_value, n.data_time ")
        strSql.Append(" from doc_summary_data n ")
        strSql.Append(" join nur_doc_status t4 ")
        strSql.Append(" on t4.doc_id = (select max(t3.doc_id) ")
        strSql.Append(" from NUR_DOC_INDEX t3 ")
        strSql.Append(" where t3.doc_setid = n.doc_id) ")
        strSql.Append(" and t4.doc_status <> '2' ")
        strSql.Append(" where n.doctype_id = '684783' ")
        strSql.Append(" and (n.data_time - to_date('2013/9/1 0:00:00', 'yyyy-mm-dd hh24:mi:ss')) >= 0 ")
        strSql.Append(" and (to_date('2013/9/10 0:00:00', 'yyyy-mm-dd hh24:mi:ss') - n.data_time) > - 1 ")
        strSql.Append(" and (n.data_name = '跌倒危险性评分' Or  n.data_name = '跌伤等级') ")
        strSql.Append(" order by n.patient_id ")
        ' 	showmessage(strSql.tostring())
        Dim dsFallInfos As DataSet = Nothing
        Me.ExecuteQuery(strSql.ToString(), dsFallInfos)
        If dsFallInfos Is Nothing OrElse dsFallInfos.Tables.Count <= 0 _
        OrElse dsFallInfos.Tables(0).Rows.Count <= 0 Then
            Return Nothing
        End If
        For index As Integer = 0 To dtPatientInfos.Rows.Count - 1
            Dim szPid As String = GetFieldValue(dtPatientInfos, index, "patient_id")
            Dim szVid As String = GetFieldValue(dtPatientInfos, index, "visit_id")
            For index1 As Integer = 0 To dsFallInfos.Tables(0).Rows.Count - 1
                If szPid = GetFieldValue(dsFallInfos.Tables(0), index1, "patient_id") _
                And szVid = GetFieldValue(dsFallInfos.Tables(0), index1, "visit_id") Then
                    If GetFieldValue(dsFallInfos.Tables(0), index1, "data_name") = "跌倒危险性评分" Then
                        dtPatientInfos.Rows(index).Item(4) _
                        = GetFieldValue(dsFallInfos.Tables(0), index1, "data_value")
                        dtPatientInfos.Rows(index).Item(5) = "1"
                    ElseIf GetFieldValue(dsFallInfos.Tables(0), index1, "data_name") = "跌伤等级" Then
                        dtPatientInfos.Rows(index).Item(6) _
                        = GetFieldValue(dsFallInfos.Tables(0), index1, "data_value")
                        dtPatientInfos.Rows(index).Item(5) = "1"
                    End If
                    ShowMessage("")
                End If
            Next index1
        Next index
        Return dtPatientInfos
    End Function

    Private Function GetPatientInfos() As DataTable
        Dim strSqlCondition As String = Me.GetSearchCondition()
        Dim XDateTimeStartTime As XDateTime = Me.FindControl("dtVisitStartTime")
        Dim XDateTimeEndTime As XDateTime = Me.FindControl("dtVisitEndTime")
        Dim strSql As New StringBuilder
        '    showmessage(strSqlCondition)
        '根据查询条件读取病人数据
        strSql.Append(" select t.WARD_CODE, t.WARD_NAME,t.PATIENT_ID, ")
        strSql.Append(" t.VISIT_ID,null FallRiskScore,null FallReport,null 跌倒前已列为已跌倒, ")
        strSql.Append(" null FallClass from pat_visit_v t ")
        strSql.Append(" where  t.DISCHARGE_TIME between ")
        strSql.Append(" to_date('" + XDateTimeStartTime.value.tostring + "', 'yyyy-mm-dd hh24:mi:ss') and ")
        strSql.Append(" to_date('" + XDateTimeEndTime.value.tostring + "', 'yyyy-mm-dd hh24:mi:ss') ")
        '   	strSql.Append(strSqlCondition)
        '   	strSql.Append(" and t.WARD_CODE='HL0501' ")
        strSql.Append(" order by t.WARD_CODE ")
        ' 	showmessage(strSql.tostring())
        Dim dsPatientInfos As DataSet = Nothing
        Me.ExecuteQuery(strSql.ToString(), dsPatientInfos)
        If dsPatientInfos Is Nothing OrElse dsPatientInfos.Tables.Count <= 0 _
        OrElse dsPatientInfos.Tables(0).Rows.Count <= 0 Then
            Return Nothing
        End If
        Return dsPatientInfos.Tables(0)
    End Function

    Private Function GetSearchCondition() As String
        Dim strSqlCondition As String = ""
        Dim control As Control
        Dim dtVisitStartTime As Date
        Dim dtVisitEndTime As Date
        '查询条件值获取
        '当前选中的护理单元
        Dim dtCurrentWardInfos As DataTable = Nothing
        control = Me.FindControl("XTextBox1")
        If control IsNot Nothing Then
            dtCurrentWardInfos = control.Tag
        End If
        Dim strWardCode As String = ""
        If dtCurrentWardInfos IsNot Nothing Then
            For Each dr As DataRow In dtCurrentWardInfos.Rows
                If strWardCode = "" Then
                    strWardCode = "'" + Me.GetFieldValue(dr, 0).ToString() + "'"
                Else
                    strWardCode += ",'" + Me.GetFieldValue(dr, 0).ToString() + "'"
                End If
            Next
        End If
        If strWardCode <> "" Then
            strSqlCondition = " t.ward_code in (" + strWardCode + ") "
        End If

        Dim XDateTimeStartTime As XDateTime = Me.FindControl("dtVisitStartTime")
        If XDateTimeStartTime IsNot Nothing Then
            dtVisitStartTime = XDateTimeStartTime.value
        End If

        control = Me.FindControl("dtVisitEndTime")
        If control IsNot Nothing Then
            dtVisitEndTime = CType(control, XDateTime).value
        End If

        If strSqlCondition = "" Then
            strSqlCondition = " t.DISCHARGE_TIME between  to_date('" + CStr(dtVisitStartTime) + "','yyyy-mm-dd hh24:mi:ss') and to_date('" + CStr(dtVisitEndTime) + "','yyyy-mm-dd hh24:mi:ss')"
        Else
            strSqlCondition = strSqlCondition + " and  t.DISCHARGE_TIME between  to_date('" + CStr(dtVisitStartTime) + "','yyyy-mm-dd hh24:mi:ss') and to_date('" + CStr(dtVisitEndTime) + "','yyyy-mm-dd hh24:mi:ss')"
        End If

        If strSqlCondition = "" Then
            ShowMessage("请选择查询条件!")
        End If
        Return strSqlCondition
    End Function

    '双击组件时发生。
    Private Sub XTextBox1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        '调用科室选择窗口
        Dim control As Control = Me.FindControl("XTextBox1")
        '当前选中的护理单元
        Dim dtCurrentWardInfos As DataTable = Nothing
        If control IsNot Nothing Then
            dtCurrentWardInfos = control.Tag
        End If
        '传出选择的护理单元
        Dim strDeptNames As String = ""
        Me.RaiseCustomEvent(sender, "病区选择对话框", "true", dtCurrentWardInfos, dtCurrentWardInfos)
        If dtCurrentWardInfos IsNot Nothing Then
            For Each dr As DataRow In dtCurrentWardInfos.Rows
                If strDeptNames = "" Then
                    strDeptNames = Me.GetFieldValue(dr, 1)
                Else
                    strDeptNames += ";" + Me.GetFieldValue(dr, 1)
                End If
            Next
        End If
        If strDeptNames <> "" Then
            Me.SetControlText("XTextBox1", strDeptNames)
        Else
            Me.SetControlText("XTextBox1", "全院<可双击修改>")
        End If
        If control IsNot Nothing Then
            control.Tag = dtCurrentWardInfos
        End If
        dtDepts = dtCurrentWardInfos
    End Sub

    '单击组件时发生。
    Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim grid As XDataGrid = CType(Me.FindControl("XDataGrid1"), XDataGrid)

        '    Dim dataset As New XDataGrid 
        Dim dt As DataTable = Nothing
        dt = Me.GetDataTable(grid, 0, 0)
        If Me.ExportCsvFile(dt) = True Then
            ShowMessage("数据导出成功!")
        End If
    End Sub

    '单击组件时发生。
    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Refresh()
    End Sub

    '单击组件时发生。
    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim grid As XDataGrid = Me.FindControl("XDataGrid1")
        grid.Rows.Clear()
    End Sub



End Class
